/**
 *登录服务
 */
import {Injectable} from '@angular/core';
import {BaseService} from '@core/routes-services/base.service';
import {CacheService} from '@core/routes-services/cache.service';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class LoginService extends BaseService {

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }


  /**
   * 登录
   *
   * @param user
   * @returns {Promise<any>}
   */
  login(user): Promise<any> {
    // const url = 'v0.1/users/all';
    // const resultPromise = this.get(url);
    const url = 'login';
    const resultPromise = this.post(url, user);
    // resultPromise.th en((data) => {
    //   this.cacheService.setTokenInfo(data);
    // });
    return resultPromise;
  }

  logout(): Promise<any> {
    const url = 'logout';
    return this.post(url, null);
  }

  getUserInfo(): Promise<any> {
    const url = 'v0.1/users/1'; // TODO 改成对应接口
    const resultPromise = this.get(url);
    resultPromise.then((data) => {
      this.cacheService.setCurrUserInfo(data);
    });
    return resultPromise;
  }

}
